﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AddSiteColumnToContentType.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Adds a site column to a content type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Actions.Wss.Column
{
    using System.Collections.Generic;

    using Entities;

    using Engine;

    using SharePoint.Wss.Attributes;

    using Utils;
    using SharePoint.Wss.Columns;

    [CollaborisClass(
        Title = "Add a Site Column to Content Type",
        ReleaseStatus = ReleaseStatus.Beta,
        DisableDocumentation = false,
        ContactAuthorEmail = "Support@Collaboris.co.uk",
        Summary = "Adds a site column to an existing content type.",
        Remarks = "The Undo will attempt to remove the site column from the list.",
        UndoImplemented = true,
          ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C627}")]
    public class AddSiteColumnToContentType : WSSActionBase<AddSiteColumnToContentTypeInfo>
    {
        #region Helper Methods

        /// <summary>
        /// Executes the Action using the values passed in by the Data Entity
        /// and the <see cref="SafContext"/>.
        /// </summary>
        /// <param name="context">
        /// A valid <see cref="SafContext"/> containing properties to run the Action with.
        /// </param>
        /// <param name="entity">
        /// A Valid <see cref="AddSiteColumnToListInfo"/> containing the parameters with which to run the Action with.
        /// </param>
        /// <returns>
        /// A <see cref="ProcessStatus"/> signifying the outcome of processing the Action.
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, AddSiteColumnToContentTypeInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");
            ValidationHelper.VerifyObjectArgument(context.Site, "context.Site");
            
            this.Trace.TraceVerbose("Adding this SiteColumn {0} to this ContenType {1}", entity.FieldName, entity.ContentTypeName);
            ProcessStatus status = ProcessStatus.Success;

            bool bSuccess = SiteColumnHelper.AddSiteColumnToContentType(TargetWeb, entity.FieldName, entity.ContentTypeName);
            if (!bSuccess)
            {
                status = ProcessStatus.Error;
            }            
            
            this.Trace.TraceMethodEnd("DoAction");
            return status;
        }

        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">
        /// The <see cref="SafContext"/> passed in by the SAF Engine.
        /// </param>
        /// <param name="entity">
        /// The data entity to Validate.
        /// </param>
        /// <param name="errors">
        /// List of <see cref="ValidationError"/>s
        /// </param>
        /// <param name="mode">
        /// <see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.
        /// </param>
        /// <returns>
        /// false if invalid; otherwise true.
        /// </returns>
        protected override bool IsValid(
            SafContext context, AddSiteColumnToContentTypeInfo entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.ContentTypeName, "AddSiteColumnToListInfo.ContentTypeName", ref errors);
            ValidateStringForNull(entity.FieldName, "AddSiteColumnToListInfo.FieldName", ref errors);

            // Have we added errors ?
            return errors.Count == currentCount;
        }

        /// <summary>
        /// Executes the Action(Removes the SiteColumn from the list) using the values passed in by the Data Entity
        /// and the <see cref="SafContext"/>.
        /// </summary>
        /// <param name="context">
        /// A valid <see cref="SafContext"/> containing properties to run the Action with.
        /// </param>
        /// <param name="entity">
        /// A Valid <see cref="AddSiteColumnToListInfo"/> containing the parameters with which to run the Action with.
        /// </param>
        /// <returns>
        /// A <see cref="ProcessStatus"/> signifying the outcome of processing the Action.
        /// </returns>
        protected override ProcessStatus UndoAction(SafContext context, AddSiteColumnToContentTypeInfo entity)
        {
            this.Trace.TraceMethodStart("UndoAction");
            ValidationHelper.VerifyObjectArgument(context.Site, "context.Site");

            this.Trace.TraceVerbose("Removing this SiteColumn {0} from this ContenType {1}", entity.FieldName, entity.ContentTypeName);

            ProcessStatus status = ProcessStatus.Success;

            bool bSuccess = SiteColumnHelper.RemoveSiteColumnFromContentType(TargetWeb, entity.FieldName, entity.ContentTypeName);
            if (!bSuccess)
            {
                status = ProcessStatus.Error;
            }           

            this.Trace.TraceMethodEnd("UndoAction");
            return ProcessStatus.Success;
        }

        #endregion
    }
}